package com.flower8.controller;

import com.flower8.model.HomeInsurer;
import com.flower8.model.HomeLoan;
import com.flower8.model.HomeWealth;
import com.flower8.service.HomeService;
import com.flower8.sysconfig.constants.MsgConstants;
import com.flower8.utils.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 首页模块
 * @Author: tanglin
 * @Date: 2020/3/27 15:12
 * @Version: 1.0
 */
@RestController
@RequestMapping("/home")
public class HomeController extends BaseController{
    private static final Logger log = LoggerFactory.getLogger(HomeController.class);

    @Autowired
    private HomeService homeService;

    /**
     * 保险精选列表
     * @param request
     * @return
     */
    @RequestMapping("/getInsurerList")
    public Object getInsurerList(HttpServletRequest request){
        logBefore(log, "++++++++++ 保险精选列表 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<HomeInsurer> dataList =  homeService.getInsurerList(pd);
            int total = homeService.getInsurerListCount(pd);
            res.put("data", dataList);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     *  根据id查询
     * @param request
     * @return
     */
    @RequestMapping("/getInsurerById")
    public Object getInsurerById(HttpServletRequest request){
        logBefore(log, "++++++++++  根据id查询 ++++++++++");
        PageData pd = new PageData(request);

        Map res = new HashMap();
        try {
            HomeInsurer homeInsurer =  homeService.getInsurerById(pd);
            res.put("insurer", homeInsurer);
            logAfter(log);
            return responseResultForSuccess(res);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 更新/新增保险精选
     * @param homeInsurer
     * @return
     */
    @RequestMapping("/updateInsurer")
    public Object updateInsurer(@RequestBody HomeInsurer homeInsurer){
        try {
            if(homeInsurer.getId() != null){
                logBefore(log, "++++++++++ 更新保险精选接口 ++++++++++");
                homeService.updateInsurer(homeInsurer);
            }else{
                logBefore(log, "++++++++++ 新增保险精选接口 ++++++++++");
                homeService.addInsurer(homeInsurer);
            }

            logAfter(log);
            return responseResultForSuccess(null, "操作成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("操作失败");
        }
    }


    /**
     * 删除保险精选
     * @param id
     * @return
     */
    @RequestMapping("/deleteInsurer")
    public Object deleteInsurer(@RequestParam long id){
        logBefore(log, "++++++++++ 删除保险精选 ++++++++++");
        try {
            homeService.deleteInsurer(id);
            logAfter(log);
            return responseResultForSuccess(null, "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("删除失败");
        }
    }


    /**
     * 闪电贷款列表
     * @param request
     * @return
     */
    @RequestMapping("/getLoanList")
    public Object getLoanList(HttpServletRequest request){
        logBefore(log, "++++++++++ 闪电贷款列表 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<HomeLoan> dataList =  homeService.getLoanList(pd);
            int total = homeService.getLoanListCount(pd);
            res.put("data", dataList);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     *  根据id查询
     * @param request
     * @return
     */
    @RequestMapping("/getLoanById")
    public Object getLoanById(HttpServletRequest request){
        logBefore(log, "++++++++++  根据id查询 ++++++++++");
        PageData pd = new PageData(request);

        Map res = new HashMap();
        try {
            HomeLoan homeLoan =  homeService.getLoanById(pd);
            res.put("loan", homeLoan);
            logAfter(log);
            return responseResultForSuccess(res);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 更新/新增闪电贷款
     * @param homeLoan
     * @return
     */
    @RequestMapping("/updateLoan")
    public Object updateLoan(@RequestBody HomeLoan homeLoan){
        try {
            if(homeLoan.getId() != null){
                logBefore(log, "++++++++++ 更新闪电贷款接口 ++++++++++");
                homeService.updateLoan(homeLoan);
            }else{
                logBefore(log, "++++++++++ 新增闪电贷款接口 ++++++++++");
                homeService.addLoan(homeLoan);
            }

            logAfter(log);
            return responseResultForSuccess(null, "操作成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("操作失败");
        }
    }


    /**
     * 删除闪电贷款
     * @param id
     * @return
     */
    @RequestMapping("/deleteLoan")
    public Object deleteLoan(@RequestParam long id){
        logBefore(log, "++++++++++ 删除闪电贷款 ++++++++++");
        try {
            homeService.deleteLoan(id);
            logAfter(log);
            return responseResultForSuccess(null, "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("删除失败");
        }
    }

    /**
     * 首页理财列表
     * @param request
     * @return
     */
    @RequestMapping("/getWealthList")
    public Object getWealthList(HttpServletRequest request){
        logBefore(log, "++++++++++ 首页理财列表 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<HomeWealth> dataList =  homeService.getWealthList(pd);
            int total = homeService.getWealthListCount(pd);
            res.put("data", dataList);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     *  根据id查询
     * @param request
     * @return
     */
    @RequestMapping("/getWealthById")
    public Object getWealthById(HttpServletRequest request){
        logBefore(log, "++++++++++  根据id查询 ++++++++++");
        PageData pd = new PageData(request);

        Map res = new HashMap();
        try {
            HomeWealth homeLoan =  homeService.getWealthById(pd);
            res.put("wealth", homeLoan);
            logAfter(log);
            return responseResultForSuccess(res);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 更新/新增首页理财
     * @param homeWealth
     * @return
     */
    @RequestMapping("/updateWealth")
    public Object updateWealth(@RequestBody HomeWealth homeWealth){
        try {
            if(homeWealth.getId() != null){
                logBefore(log, "++++++++++ 更新首页理财接口 ++++++++++");
                homeService.updateWealth(homeWealth);
            }else{
                logBefore(log, "++++++++++ 新增首页理财接口 ++++++++++");
                homeService.addWealth(homeWealth);
            }

            logAfter(log);
            return responseResultForSuccess(null, "操作成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("操作失败");
        }
    }


    /**
     * 删除首页理财
     * @param id
     * @return
     */
    @RequestMapping("/deleteWealth")
    public Object deleteWealth(@RequestParam long id){
        logBefore(log, "++++++++++ 删除首页理财 ++++++++++");
        try {
            homeService.deleteWealth(id);
            logAfter(log);
            return responseResultForSuccess(null, "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("删除失败");
        }
    }

}
